package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.PageResult;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.BusinessTrackRecordMapper;
import com.qk.service.BusinessService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;

    @Override
    public PageResult<Business> list(BusinessDto businessDto) {
        Page<Business> businessPage = this.businessMapper.list(new Page<Business>( businessDto.getPage(),businessDto.getPageSize()), businessDto);
        return new PageResult<>(businessPage.getTotal(),businessPage.getRecords());
    }

    @Override
    public Business gettrackRecordById(Integer id) {
        Business business = businessMapper.gettrackRecordById(id);
        return business;
    }
    @Transactional
    @Override
    public void trackRecord(Business business) {
        business.setStatus(3);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);

        BusinessTrackRecord businessTrackRecord = new BusinessTrackRecord();
        businessTrackRecord.setBusinessId(business.getId());
        businessTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        businessTrackRecord.setKeyItems(business.getKeyItems().toString());
        businessTrackRecord.setTrackStatus(business.getTrackStatus());
        businessTrackRecord.setNextTime(business.getNextTime());
        businessTrackRecord.setCreateTime(LocalDateTime.now());
        businessTrackRecord.setRecord(business.getRecord());
        businessTrackRecordMapper.insert(businessTrackRecord);

    }
}
